const router = require('koa-router')()
const cc = require('../conCtl')
const js = require('../jsCtl')
const db = require('../dbCtl')
const { now } = require('moment')

/**
@hostpath = http://127.0.0.1/service/order
 */

/** 获取订单列表
###
    GET {{hostpath}}?page=1&size=30
### */
router.get('/', async c => {
  let
    page = c.query.page || 1,
    size = c.query.size || 30;

  let data = await db.model('t_order a')
    .join('t_users b on a.openid=b.openid')
    .join('t_goods c on a.goods_id=c.id')
    .field('a.*,b.nick,b.tel,b.headurl,c.id as goods_id,c.name,a.money')
    .orderBy('a.dt desc')
    .select(page, size);
  for (let i = 0; i < data.length; i++) {
    const item = data[i];
    if (item.wuliuc_id) item.wuliuc_id = await db.model('t_wuliu_company').where('id=?', item.wuliuc_id).shift('', 'name');
    if (item.wuliu_id) item.wuliu_id = await db.model('t_wuliu').where('id=?', item.wuliu_id).shift();
  }

  let count = await db.model('t_order').count();

  cc.send_list(c, data, count);
})

/** 获取所有物料公司数据
###
    GET {{hostpath}}/logistics
### */
router.get('/logistics', async c => {

  let data = await db.model('t_wuliu_company').where('status=1').select();
  cc.send_list(c, data);
})

/** 发货
###
    POST {{hostpath}}/send
    Content-Type: application/json

    {
        "order_id":1,
        "wuliuc_id":20,
        "wuliu_number":"12412"
    }
### */
router.post('/send', async c => {
  js.check_params(
    !c.request.body.wuliuc_id && '.物流公司' ||
    !c.request.body.wuliu_number && '?物流单号');
  //获取订单数据
  let order = await db.model('t_order').where('order_id=?', c.request.body.order_id).shift('订单错误');
  //获取用户地址数据
  let user_address = await db.model('t_user_address').where('address_id=?', order.address_id).shift('用户地址错误');
  let address = user_address.province + user_address.city + user_address.area + user_address.address;//拼接收货地址
  let time = new Date();


  await db.startTransaction(async (tran) => {
    //新增物流数据
    let wuliu_id = await db.model('t_wuliu')
      .insert({ wuliuc_id: c.request.body.wuliuc_id, no: c.request.body.wuliu_number, tel: user_address.phone, name: user_address.name, address: address });
    //修改订单数据
    await db.model('t_order').where('order_id=?', c.request.body.order_id).update({ state: 4, wuliuc_id: c.request.body.wuliuc_id, wuliu_id: wuliu_id, delivery_time: time });
  })

  cc.send(c);
})

/** 编辑发货
###
    POST {{hostpath}}/upd_send
    Content-Type: application/json

    {
        "order_id":1,
        "wuliuc_id":20,
        "no":"12412",
        "tel":"",
        "name":"",
        "address":""
    }
### */
router.post('/upd_send', async c => {
  js.check_params(
    !c.request.body.wuliuc_id && '.物流公司' ||
    !c.request.body.no && '?物流单号' ||
    !c.request.body.tel && '?联系电话' ||
    !c.request.body.name && '?收件人' ||
    !c.request.body.address && '?收货地址');
  //获取订单数据
  let order = await db.model('t_order').where('order_id=?', c.request.body.order_id).shift('订单错误');



  await db.startTransaction(async (tran) => {
    //新增物流数据
    await tran.model('t_wuliu').where('id=?', order.wuliu_id)
      .update({ wuliuc_id: c.request.body.wuliuc_id, no: c.request.body.no, tel: c.request.body.tel, name: c.request.body.name, address: c.request.body.address });
    //修改订单数据
    await tran.model('t_order').where('order_id=?', c.request.body.order_id).update({ wuliuc_id: c.request.body.wuliuc_id });
  })

  cc.send(c);
})


module.exports = router